from __future__ import absolute_import
import logging
from os.path import join
from forest.api import Command, get_tool

logger = logging.getLogger('forest.commands.Locale')

class Locale(Command):
    ''' Sets the language, default font, and other languages.

        Example::

            locale:
                language: en_us.UTF-8
                font:latarcyrheb-sun16

        .. describe:: locale.language

            Sets the system language.

        .. describe:: locale.supported

            Specifies other available languages. Optional, Unsupported.

        .. describe:: locale.font

            Sets the system font.
    '''
    __register__ = 'commands.setup.Locale'

    requires_config = ['locale']

    def run(self, config):

        self.args = config.get('locale')

        if not self.args:
            return

        language = self.args.get('language', None)
        font = self.args.get('font', None)
        install_tree = config.get_install_root()
        loc_path = config.join_install_root( 'etc', 'sysconfig', 'i18n')

        locale_cfg = []

        #TODO I think there was a shortcut for both these settings before,
        # but what remained of in the file it was def not functional
        if language is not None:
            locale_cfg.append('LANG="{0}"'.format(language))
        if font is not None:
            locale_cfg.append('SYSFONT="{0}"'.format(font))


        locale_cfg.append('')
        get_tool('files.write')(loc_path, '\n'.join(locale_cfg))
